package com.liujit.bms.pc.controller;

import com.liujit.bms.pc.domain.query.BlogArticleQuery;
import com.liujit.bms.pc.domain.result.BlogArticleResult;
import com.liujit.bms.pc.domain.result.BlogCategoryResult;
import com.liujit.bms.pc.service.BlogArticleService;
import com.liujit.bms.pc.service.BlogCategoryService;
import com.liujit.common.domain.result.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Description 博客分类管理接口
 * @Author liujun
 * @Date 2021-07-03 16:03:30
 */
@RequiredArgsConstructor
@Api(tags = "博客分类管理接口")
@Controller
@RequestMapping("/blog/category")
public class BlogCategoryController {

    private final BlogCategoryService blogCategoryService;

    private final BlogArticleService blogArticleService;

    @ApiOperation(value = "列表查询", notes = "根据博客分类查询条件，查询博客分类列表")
    @GetMapping
    public String getList(Model model) {
        List<BlogCategoryResult> blogCategoryList = blogCategoryService.getList();
        model.addAttribute("category", blogCategoryList);
        return "category";
    }

    @RequestMapping(value = {"/{id}"})
    public String articleList(Model model, @PathVariable Long id, Integer page) {
        BlogArticleQuery query = new BlogArticleQuery();
        query.setPageNumber(page);
        query.setCategoryId(id);

        PageBean<BlogArticleResult> pageBean = blogArticleService.getPage(query);
        model.addAttribute("total", pageBean.getTotal());
        model.addAttribute("page", pageBean.getPageNumber());
        model.addAttribute("pages", pageBean.getPages());
        model.addAttribute("articleList", pageBean.getRows());

        BlogCategoryResult category = blogCategoryService.get(id);
        model.addAttribute("category", category);

        return "categoryArticle";
    }
}
